const util = require('../../utils/util.js');
const api = require('../../config/api.js');

//获取应用实例
var app = getApp()


Page({

  data: {
    id: null,
    name: '',
    price: 0,
    navbar_bgcolor: wx.getStorageSync('navbar_bgcolor'),
    detail:{},
    disable:false
  },

  onLoad: function (e) {
    var that = this;

    var userid = wx.getStorageSync("userid");
    if(userid == ''){
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }
    util.request(api.getClassTypeInfo, {uid:userid}).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          detail:res.data
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
  },

  bindCancel: function () {
    wx.navigateBack({
      delta: 1,
    })
  },


  bindSave: function (e) {
    var that = this;
    
    that.setData({
      disable:true
    });
    wx.showLoading({
      title: '支付中...',
    })

    var userid = wx.getStorageSync("userid");
    if(userid == ''){
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }
    util.request(api.getPayParam, {uid:userid})
      .then(function (res) {
        console.log(JSON.stringify(res));
        wx.hideLoading()
        if (res.errno == 0) {
          // 请求支付参数
          var action = res.data.action;
          var payInfo = res.data.bean;
          that.pay(action, payInfo);
        } else {
          wx.showModal({
            title: '提示',
            content: res.errmsg,
            showCancel: false
          })
        }
      }).catch(error => {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: error.errmsg,
          showCancel: false
        })
      });


  },
  
  //调起微信支付
  pay: function (action, data) {
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.packages,
      signType: data.signType,
      paySign: data.paySign,
      success: function (res) {
        //util.subscribeMessage();
        wx.navigateTo({
          url: '/pages/payResult/payResult?status=true',
        })
      },
      fail: function (res) {
        wx.navigateTo({
          url: '/pages/payResult/payResult?status=false',
        })
      },
      complete: function (res) {
      }
    })
  
  }

})